//
//  UserNBASession.swift
//  Sports
//
//  Created by tiger on 2017/11/7.
//  Copyright © 2017年 Meigabay. All rights reserved.
//

import UIKit

private let NBAClubIdKey = "NBAClubIdKey"

public class NBAUser {

    public static let shared: NBAUser = NBAUser()
    
    init() {}
    
    public var club: NBAClub!
    
    //开始方法
    public func startPlay(success: ()->Swift.Void,failed: (Error)->Void) {
        //创建数据库
        NBADBManager.shared.createTables(success: {
            
        }) { (error) in
            failed(error)
            return
        }
        
        //启动NBA
        NBA.shared.start()
        
        //初始化个人信息
        if let clubId = UserDefaults.standard.string(forKey: NBAClubIdKey) {
            if let club = NBA.shared.clubManagerDept.getClubById(clubId: clubId) {
                self.club = club
            }
        } else {
            NBA.shared.personnelDept.initPersonnelData()
        }
        
     
        success()
    }
    
    //是否拥有了俱乐部
    public func hasClub() -> Bool {
        if self.club != nil {
            return true
        }
        return false
    }
    
    /// 初始化玩家的俱乐部
    ///
    /// - Parameter club: 俱乐部信息
    public func initClub(club: NBAClub) throws {
        do {
            try self.club = NBA.shared.clubManagerDept.createClub(club: club)
            UserDefaults.standard.setValue(self.club!.clubId, forKey: NBAClubIdKey)
            NotificationCenter.default.post(name: NSNotification.Name.NBAUserClubDidInit, object: nil)
        } catch {
            throw(error)
        }
    }
   
}
